package com.siemens.industrial.web.mvc.controller.partner.user;

import com.siemens.industrial.model.partner.Event;
import com.siemens.industrial.model.partner.User;
import com.siemens.industrial.utils.*;
import com.siemens.industrial.web.mvc.pojo.DataTableQuery;
import com.siemens.industrial.web.mvc.service.AuthService;
import com.siemens.industrial.web.mvc.service.admin.TenantService;
import com.siemens.industrial.web.mvc.service.partner.PartnerDeviceService;
import com.siemens.industrial.web.mvc.service.partner.PartnerEventService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by qupen on 2016/10/17.
 */
@Controller
public class PartnerEventController {
    @Resource
    PartnerEventService partnerEventService;

    @Resource
    PartnerDeviceService partnerDeviceService;

    @Resource
    TenantService tenantService;

    @Resource
    AuthService authService;

    @RequestMapping(value = "/partner/{tenant}/event/list", method = RequestMethod.GET)
    public String getCreateUserPage(@PathVariable("tenant") String tenantKey, ModelMap modelMap) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        if (tenantId == null)
            return "error";
        modelMap.put("key", tenantKey);
        return "partner/user/eventSub_list";
    }

    @RequestMapping(value = "/partner/{tenant}/event/create", method = RequestMethod.GET)
    public String getCreateEventPage(@PathVariable("tenant") String tenantKey, ModelMap modelMap) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        if (tenantId == null)
            return "error";
        modelMap.put("key", tenantKey);
        return "partner/user/createEvent";
    }

    //事件订阅列表
    @RequestMapping(value = "/partner/{tenant}/api/v1/event/list", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getEventList(@PathVariable("tenant") String tenantKey) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        List<Map<String, Object>> events = partnerEventService.getEventSubscriptions(tenantId);

        return RestResponse.create(GlobalResponseCode.SUCCESS)
                .putData(events)
                .build();
    }

    @RequestMapping(value = "/partner/{tenant}/api/v1/event/subList", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getEventList(@PathVariable("tenant") String tenantKey,
                                            @RequestParam(name = "deviceId") String deviceId) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        List<Map<String, Object>> events = partnerEventService.getEventSubscriptionsByDeviceId(tenantId, deviceId);

        return RestResponse.create(GlobalResponseCode.SUCCESS)
                .putData(events)
                .build();
    }

    @RequestMapping(value = "/partner/{tenant}/api/v1/event/delete", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deleteEvent(@PathVariable("tenant") String tenantKey, @RequestParam(name = "eventId") String eventId) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        partnerEventService.deleteEventAndRelation(tenantId, eventId);

        return RestResponse.create(GlobalResponseCode.SUCCESS).build();
    }

    @RequestMapping(value = "/partner/{tenant}/event/subscription", method = RequestMethod.GET)
    public String getCreateSubPage(@PathVariable("tenant") String tenantKey, ModelMap modelMap) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        if (tenantId == null)
            return "error";
        List<String> types = partnerDeviceService.getDeviceType(tenantId);
        List<Event> list = partnerEventService.getEvents(tenantId);
        List<Map<String, Object>> groups = partnerDeviceService.getAllDistinctGroups(tenantId);
        List<Map<String, Object>> lines = partnerDeviceService.getAllDistinctLines(tenantId);
        modelMap.put("types", types);
        modelMap.put("events", list);
        modelMap.put("groups", groups);
        modelMap.put("lines", lines);
        return "partner/user/createSubscription";
    }

    @RequestMapping(value = "/partner/{tenant}/api/v1/even/create", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> saveEvent(@PathVariable("tenant") String tenantKey, @RequestBody Map<String, Object> data) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        partnerEventService.saveDeviceEventRelation(tenantId, data);

        return RestResponse.create(GlobalResponseCode.SUCCESS).build();
    }

    @RequestMapping(value = "/partner/{tenant}/api/v1/event/add", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> addEvent(@PathVariable("tenant") String tenantKey, @RequestBody Map<String, Object> data) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        Event event = new Event();
        event.setEventId(UUIDGenerator.getUUID());
        event.setEventDesc(data.get("description").toString());
        event.setEventName(data.get("name").toString());
        event.setEventType(data.get("type").toString());
        event.setEventTrigger(JsonToOtherFormat.buildJsonToString(data.get("trigger")));
        try {
            partnerEventService.saveEvent(tenantId, event);
        } catch (SiemensException e) {
            e.printStackTrace();
        }

        return RestResponse.create(GlobalResponseCode.SUCCESS).build();
    }

    @RequestMapping(value = "/partner/{tenant}/event/events", method = RequestMethod.GET)
    public String getEventListPage(@PathVariable("tenant") String tenantKey, ModelMap modelMap) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        if (tenantId == null)
            return "error";
        modelMap.put("key", tenantKey);
        return "partner/user/event_list";
    }

    @RequestMapping(value = "/partner/{tenant}/api/v1/event/evens", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getEvents(@PathVariable("tenant") String tenantKey) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        List<Event> events = partnerEventService.getEvents(tenantId);
        return RestResponse.create(GlobalResponseCode.SUCCESS)
                .putData(events)
                .build();
    }

    @RequestMapping(value = "/partner/{tenant}/api/v1/event/getOne", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getEvent(@PathVariable("tenant") String tenantKey, @RequestParam(name = "eventId") String eventId) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        Event event = partnerEventService.getEvent(tenantId, eventId);
        return RestResponse.create(GlobalResponseCode.SUCCESS)
                .putData(event)
                .build();
    }

    @RequestMapping(value = "/partner/{tenant}/event/edit", method = RequestMethod.GET)
    public String getEventEditPage(@PathVariable("tenant") String tenantKey, String eventId, ModelMap modelMap) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        if (tenantId == null)
            return "error";
        modelMap.put("key", tenantKey);
        modelMap.put("eventId", eventId);
        return "partner/user/edit_event";
    }

    @RequestMapping(value = "/partner/{tenant}/api/v1/event/edit", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> editEvent(@PathVariable("tenant") String tenantKey, @RequestBody Map<String, Object> data) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        try {
            partnerEventService.updateEvent(tenantId, data);
            return RestResponse.create(GlobalResponseCode.SUCCESS).build();
        } catch (SiemensException e) {
            e.printStackTrace();
            return RestResponse.create(GlobalResponseCode.ERROR_EVENT_NOT_EXISTED).build();
        }
    }

    @RequestMapping(value = "/partner/{tenant}/api/v1/event/deleteEvent", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> editEvent(@PathVariable("tenant") String tenantKey, @RequestParam(name = "eventId") String eventId) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        try {
            partnerEventService.deleteEvent(tenantId, eventId);
            return RestResponse.create(GlobalResponseCode.SUCCESS).build();
        } catch (SiemensException e) {
            e.printStackTrace();
            return RestResponse.create(GlobalResponseCode.ERROR_EVENT_NOT_EXISTED).build();
        }
    }

    @RequestMapping(value = "/partner/{tenant}/api/v1/event/getSub", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getSub(@PathVariable("tenant") String tenantKey, @RequestParam(name = "eventId") String eventId) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        List<Map<String, Object>> maps = partnerEventService.getAllSubByEventId(tenantId, eventId);
        return RestResponse.create(GlobalResponseCode.SUCCESS).putData(maps).build();
    }

    @RequestMapping(value = "/partner/{tenant}/even/modify", method = RequestMethod.GET)
    public String modifyDeviceEvent(@PathVariable("tenant") String tenantKey, String eventId, ModelMap modelMap) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        Event event = partnerEventService.getEvent(tenantId, eventId);
        List<Map<String, Object>> maps = partnerEventService.getAllSubByEventId(tenantId, eventId);

        modelMap.put("eventSub", maps);
        modelMap.put("event", event);
        return "partner/user/evnet_sub_update";
    }

    @RequestMapping(value = "/partner/{tenant}/api/v1/eventRelation/delete", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> modifyDeviceEvent(@PathVariable("tenant") String tenantKey,
                                                @RequestBody Map<String,Object> map) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        List <Map<String,Object>> list = (List<Map<String,Object>>) map.get("paramArr");
        String eventId = map.get("eventId").toString();
        partnerEventService.deleteEventAndRelationById(tenantId, list, eventId);
        return RestResponse.create(GlobalResponseCode.SUCCESS).build();
    }

    @RequestMapping(value = "/partner/{tenant}/api/v1/event/evensByDataTable", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getEventsByDataTable(@PathVariable("tenant") String tenantKey,
                                                    HttpServletRequest request) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        DataTableQuery dt = new DataTableQuery(request);
        if (dt.isDataTableRequest()) {
            Map<String, Object> events = partnerEventService.getEventsByDataTable(tenantId, request);
            return events;
        }
        return dt.buildResponse(0, 0, new ArrayList<>());
    }

    //事件订阅列表
    @RequestMapping(value = "/partner/{tenant}/api/v1/eventsByDataTable", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getEventList(@PathVariable("tenant") String tenantKey,
                                            HttpServletRequest request) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        DataTableQuery dt = new DataTableQuery(request);

        if (dt.isDataTableRequest()) {
            Map<String, Object> events = partnerEventService.getEventSubscriptionsByDataTable(tenantId, request);
            return events;
        }
        return dt.buildResponse(0, 0, new ArrayList<>());
    }

    @RequestMapping(value = "/partner/{tenant}/api/v1/eventSubForDevice", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getSubscriptions(@PathVariable("tenant") String tenantKey, String subscription) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        List<Map<String, Object>> isSubscriptions = partnerEventService.getAllSubscriptionByType(tenantId, subscription, 1);
        return RestResponse.create(GlobalResponseCode.SUCCESS).putData(isSubscriptions).build();
    }

    @RequestMapping(value = "/partner/{tenant}/api/v1/eventSubForGroup", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getSubscriptionsForGroup(@PathVariable("tenant") String tenantKey, String subscription) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        List<Map<String, Object>> isSubscriptions = partnerEventService.getAllSubscriptionByType(tenantId, subscription, 2);
        return RestResponse.create(GlobalResponseCode.SUCCESS).putData(isSubscriptions).build();
    }

    @RequestMapping(value = "/partner/{tenant}/api/v1/eventSubForLine", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getSubscriptionsForLine(@PathVariable("tenant") String tenantKey, String subscription) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        List<Map<String, Object>> isSubscriptions = partnerEventService.getAllSubscriptionByType(tenantId, subscription, 3);
        return RestResponse.create(GlobalResponseCode.SUCCESS).putData(isSubscriptions).build();
    }
}
